package cn.org.mrliu.ssm.web.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.fabric.xmlrpc.base.Array;

import cn.org.mrliu.ssm.common.web.controller.BaseController;
import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.constant.LogConstant;
import cn.org.mrliu.ssm.constant.RequestUrl;
import cn.org.mrliu.ssm.model.Customer;
import cn.org.mrliu.ssm.service.CustomerService;
import cn.org.mrliu.ssm.util.Pagination;

@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(CustomerController.class);
	private static final String DESCRIPTION = "Customer";
	@Resource
	private CustomerService customerService;

	/**
	 * 跳转到当前模块的主页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.INDEX)
	public String index(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "index");
		log.debug(LogConstant.MODEL + model);
		return "customer/index";
	}

	/**
	 * 根据ID获取对象数据
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.DETAIL)
	public String detail(@PathVariable("id") Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "detail");
		log.debug(LogConstant.ID, id);
		Customer customer = customerService.getModel(id);
		model.addAttribute("customer", customer);
		log.debug(LogConstant.MODEL + model);
		return "customer/detail";
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.ADD)
	public String add(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "add");
		log.debug(LogConstant.MODEL + model);
		return "customer/add";
	}

	/**
	 * 保存数据
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.SAVE)
	@ResponseBody
	public ResponseMessage save( Customer customer) throws Exception {
		Integer id = customer.getId();
		String typeName = null;
		if (id!=null) {//修改
			typeName = "修改";
		}else{ // 添加
			typeName = "添加";
		}
		
		customerService.saveOrUpdateModel(customer);
		log.debug(LogConstant.EXCUTE_METHOD, "save");
		log.debug("customer:" + customer);
		try {
			customerService.saveModel(customer);
			log.info(LogConstant.SAVE_SUCCESS, DESCRIPTION);
			return new ResponseMessage(typeName+"成功!");
		} catch (Exception e) {
			log.error(LogConstant.SAVE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, typeName+"失败!");
	}

	/**
	 * 跳转到编辑页面之前获取对象数据
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.EDIT)
	public String edit(@PathVariable("id") Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "edit");
		log.debug(LogConstant.ID, id);
		Customer customer = customerService.getModel(id);
		model.addAttribute("customer", customer);
		log.debug(LogConstant.MODEL + model);
		return "customer/edit";
	}

	/**
	 * 保存数据
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.UPDATE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage update(Customer customer) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "update");
		log.debug("customer:" + customer);
		try {
			customerService.updateModel(customer);
			log.info(LogConstant.UPDATE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.UPDATE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "修改失败!");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.DELETE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delete(@PathVariable("id") Integer id) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "delete");
		log.debug(LogConstant.ID, id);
		try {
			customerService.deleteModel(id);
			log.info(LogConstant.DELETE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("删除成功!");
		} catch (Exception e) {
			log.info(LogConstant.DELETE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "删除失败!");
	}

	/**
	 * 获取分页列表
	 * 
	 * @param queryParam
	 *            查询参数
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.LIST)
	public String list(QueryParam queryParam, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "list");
		log.debug("queryParam:" + queryParam);
		Pagination pagination = customerService.getPagination(queryParam);
		model.addAttribute("customerList", pagination);
		log.debug(LogConstant.MODEL + model);
		return "customer/list";
	}
	
	@RequestMapping("/list1")
	public ModelAndView list1(QueryParam queryParam) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "list");
		log.debug("queryParam:" + queryParam);
		Pagination pagination = customerService.getPagination(queryParam);
		ModelAndView mv = new ModelAndView();
		mv.addObject("customerList", pagination);
		mv.setViewName("customer/list");
		return mv;
	}
	
	@RequestMapping("/list2")
	public String list2(QueryParam queryParam) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "list");
		log.debug("queryParam:" + queryParam);
		Pagination pagination = customerService.getPagination(queryParam);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("customerList", pagination);
		return "customer/list";
	}

	/**
	 * 批量保存对象
	 * 
	 * @param customers
	 *            JSON数组
	 * @return
	 */
	@RequestMapping(value = RequestUrl.BATCH_SAVE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchSave(@RequestBody Customer[] customers) {
		log.debug(LogConstant.EXCUTE_METHOD, "batchSave");
		log.debug("customers:" + customers);
		try {
			customerService.batchSaveModel(customers);
			log.info(LogConstant.BATCH_SAVE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("批量保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_SAVE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "批量保存失败!");
	}

	/**
	 * 批量修改
	 * 
	 * @param customer
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.BATCH_UPDATE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchUpdate(Customer customer, String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "batchUpdate");
		log.debug(LogConstant.ID_String + ids);
		log.debug("customer:" + customer);
		try {
			String[] idStrings = handleIdString(ids);
			System.out.println(ids);
			log.debug(LogConstant.IDS + ids);
			customerService.batchUpdateModel(customer, idStrings);
			log.info(LogConstant.BATCH_UPDATE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("批量修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_UPDATE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "批量修改失败!");
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = RequestUrl.BATCH_DELETE, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchDelete(String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD, "batchDelete");
		log.debug(LogConstant.ID_String + ids);
		try {
			String[] idStrings = handleIdString(ids);
			log.debug(LogConstant.IDS + ids);
			customerService.batchDeleteModel(idStrings);
			log.info(LogConstant.BATCH_DELETE_SUCCESS, DESCRIPTION);
			return new ResponseMessage("批量删除成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_DELETE_FAILURE, DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "批量删除失败!");
	}

	@RequestMapping("/test")
	@ResponseBody
	public  Pagination test() throws Exception {
		Pagination pagination = customerService.getCustomerList(5,10,"Hello");
		System.out.println(pagination);
		return  pagination;
	}
}
